package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.SystemDictionary;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.qo.SystemDictionaryItemQuery;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import cn.wolfcode.crm.service.ISystemDictionaryService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    //查询所有
    @RequestMapping("/list")
    @RequiresPermissions(value = {"systemDictionaryItem:list", "字典明细列表"},logical = Logical.OR)
    public String list(Model model,@ModelAttribute("qo")SystemDictionaryItemQuery qo){

        PageInfo pageInfo = systemDictionaryItemService.query(qo);
        model.addAttribute("result",pageInfo);
        //目录全部数据
        List<SystemDictionary> systemDictionaries = systemDictionaryService.selectAll();
        model.addAttribute("systemDictionaries",systemDictionaries);
        return "/systemDictionaryItem/list";
    }

    //删除
    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = {"systemDictionaryItem:delete", "字典明细删除"},logical = Logical.OR)
    public JsonResult delete(Long id){
        systemDictionaryItemService.deleteByPrimaryKey(id);
        return new JsonResult();
    }

/*    @RequiredPermission(name = "字典明细编辑页面")
    @RequestMapping("/input")
    @ResponseBody
    public JsonResult input(Long id, Model model){
        try {
            if (id != null) {
                SystemDictionaryItem systemDictionaryItem = systemDictionaryItemService.get(id);
                model.addAttribute("systemDictionaryItem",systemDictionaryItem);
            }
        }catch (Exception e){
            return new JsonResult(false,"保存失败,请重新处理!");
        }
        return null;
    }*/

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"systemDictionaryItem:saveOrupdate", "字典明细新增/编辑"},logical = Logical.OR)
    public JsonResult saveOrupdate(SystemDictionaryItem systemDictionaryItem){
        try {
            if (systemDictionaryItem.getId() == null) {
                systemDictionaryItemService.save(systemDictionaryItem);
            }else{
                systemDictionaryItemService.updateByPrimaryKey(systemDictionaryItem);
            }
            return new JsonResult();
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(false,"操作失败,请重新操作!!");
        }
    }
}
